//
//  CircleIndicator.m
//  Project5
//
//  Created by Nguyen Tan Loc on 5/17/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "CircleIndicator.h"


@implementation CircleIndicator

@synthesize _Radius = m_Radius, _CIID = m_CIID;

-(id) initWithSegment:(int)segment radius:(float)radius
{
    if (self = [super init])
    {
        m_Segment = segment;
        m_Radius = radius;
        m_BatchNode = [CCSpriteBatchNode batchNodeWithFile:@"variousSprites@2x.png"];
        [self addChild:m_BatchNode];
        
        CCSprite *sprite;
        float r;
        for (int i = 0; i < m_Segment; i++)
        {
            r = 360 * i / m_Segment;
            sprite = [[Utility sharedUtility] createSpriteWithKey:@"SPRITE_TEMPLATE1"];
            [sprite setPosition:ccp(m_Radius * sinf(CC_DEGREES_TO_RADIANS(r)), m_Radius * cosf(CC_DEGREES_TO_RADIANS(r)))];
            [sprite setScale:0.5f];
            
            [m_BatchNode addChild:sprite z:1 tag:i];
        }
    }
    return self;
}

-(void) setRadius:(float)radius
{
    m_Radius = radius;
    [self calcPositionAllNode];
    
//    if (m_Radius < 100)
//    {
//        for (int i = 0; i < m_Segment; i++)
//        {
//            CCNode *node = [m_BatchNode getChildByTag:i];
//            [node setVisible:(i % 2 == 0)];
//        }
//    }
//    else
//    {
//        for (int i = 0; i < m_Segment; i++)
//        {
//            CCNode *node = [m_BatchNode getChildByTag:i];
//            [node setVisible:YES];
//        }
//    }
}

-(void) setID:(int)i
{
    m_CIID = i;
}

-(void) calcPositionAllNode
{
    float r;
    for (int i = 0; i < m_Segment; i++)
    {
        r = 360 * i / m_Segment;
        CCNode *node = [m_BatchNode getChildByTag:i];
        [node setPosition:ccp(m_Radius * sinf(CC_DEGREES_TO_RADIANS(r)), m_Radius * cosf(CC_DEGREES_TO_RADIANS(r)))];
    }
}

-(void) scaleToRadius:(float)radius fade:(BOOL)fade
{
    CCLOG(@"scaleToRadius");
    m_NextRadius = radius;
    m_Speed = (m_Radius - m_NextRadius) / 0.5f;
    if (fade)
    {
        for (int i = 0; i < m_Segment; i++)
        {
            CCSprite *sprite = (CCSprite*)[m_BatchNode getChildByTag:i];
            sprite.opacity = 0;
        }
    }
    
    [self schedule:@selector(updateScale:) interval:0.016f];
}

-(void) updateScale:(ccTime)delta
{
    CCSprite *sprite;
    
    m_Radius -= m_Speed * delta;
    
    for (int i = 0; i < m_Segment; i++)
    {
        sprite = (CCSprite*)[m_BatchNode getChildByTag:i];
        sprite.opacity += (255.0f / 0.5f) * delta;
    }
    
    if (m_Radius <= m_NextRadius)
    {
        m_Radius = m_NextRadius;
        for (int i = 0; i < m_Segment; i++)
        {
            sprite = (CCSprite*)[m_BatchNode getChildByTag:i];
            sprite.opacity = 255;
        }
        [self unschedule:@selector(updateScale:)];
    }
    
    [self calcPositionAllNode];
}

-(void) destroy
{
    CCLOG(@"destroy circle indicator");
    CCSprite *sprite;
    for (int i = 0; i < m_Segment; i++)
    {
        sprite = (CCSprite*)[m_BatchNode getChildByTag:i];
        CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:0.2f opacity:0];
        [sprite runAction:fadeTo];
    }
}


@end
